package com.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.service.ServiceImpl;

public class InsertData extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public InsertData() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		String loadType=request.getParameter("loadType");
		if(loadType==null) return;
		if(loadType.equals("1")){
			String filePath=request.getParameter("filePath");
			String type=request.getParameter("type");
			if(type.equals("1")){
				filePath="C:/Program Files (x86)/SSQ500WAN/Data/ssqHisData.lex";
			}else if(type.equals("2")){
				filePath="C:/Program Files (x86)/DLT500WAN/Data/dltHisData.lex";				
			}
			ServiceImpl.inst().loadDataFromFile(filePath, type);
		}else if(loadType.equals("2")){
			Map<String, String> map=new HashMap<String, String>();
			String type=request.getParameter("type");
			String nper=request.getParameter("nper");
			String num1=request.getParameter("num1");
			String num2=request.getParameter("num2");
			map.put("type", type);
			map.put("nper", nper);
			map.put("num1", num1);
			map.put("num2", num2);
			map.put("tableName", "data1");
			ServiceImpl.inst().insertLotteryData(map);
		}
		response.getWriter().print("OK");
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
